package com.java.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.java.model.Mail;
import com.java.model.PageBean;
import com.java.model.Status;
import com.java.model.User;
import com.java.service.MailService;
import com.java.service.StatusService;
import com.java.util.AppConfig;
import com.java.util.MailUtil;
import com.java.util.PageUtil;
import com.java.util.ResponseUtil;
import com.java.util.StringUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/status")
public class StatusController {

	@Autowired
	private StatusService statusService;

	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(value = "page", required = false) String page, Status s_status,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		HttpSession session = request.getSession();
		System.out.println(page);
		if (StringUtil.isEmpty(page)) {
			page = "1";
			session.setAttribute("s_status", s_status);
			System.out.println(s_status.getId());
		} else {
			s_status = (Status) session.getAttribute("s_status");
		}
		PageBean pageBean = new PageBean(Integer.parseInt(page), AppConfig.pageSize);
		List<Status> statusList = statusService.find(pageBean, s_status);
		if (!statusList.isEmpty()){
			mav.addObject("companyName", statusList.get(0).getCompanyName());
			mav.addObject("companyPhone", statusList.get(0).getCompanyPhone());
		}
		
		int total = statusService.count(s_status);
		String pageCode = PageUtil.getPagation(request.getContextPath() + "/status/list.do", total,
				Integer.parseInt(page), AppConfig.pageSize);
		mav.addObject("pageCode", pageCode);
		mav.addObject("modeName", "查快递");
		User user = (User) request.getSession().getAttribute("currentUser");
		if (user.getRoleName().equals("快递员")){
			mav.addObject("modeName", "快递状态管理");
		}
		mav.addObject("statusList", statusList);
		mav.addObject("mainPage", "status/list.jsp");
		mav.setViewName("main");
		return mav;
	}

	@RequestMapping("/preSave")
	public ModelAndView preSave(@RequestParam(value = "id", required = false) String id) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("mainPage", "status/save.jsp");
		mav.addObject("modeName", "快递状态管理");
		mav.setViewName("main");
		if (StringUtil.isNotEmpty(id)) {
			mav.addObject("actionName", "快递状态修改");
			Status status = statusService.loadById(Integer.parseInt(id));
			mav.addObject("status", status);
		} else {
			mav.addObject("actionName", "快递状态添加");
		}
		return mav;
	}

	@RequestMapping("/save")
	public String save(Status status) {
		if (status.getId() == null && status.getOrderId() != null) {
			statusService.add(status);
			Mail mail = new Mail();
			mail.setContext("订单号:"+status.getOrderId()+",当前进度:"+status.getStep());
			try {
				MailUtil.sendMail(AppConfig.smailName, AppConfig.rmailName, AppConfig.smailName, AppConfig.spasswd,
						mail);
			} catch (Exception e) {
				System.out.println("发送邮件失败");
			}
			System.out.println("发送邮件成功");
		} else {
			statusService.update(status);
		}
		return "redirect:/status/list.do";
	}

	@RequestMapping("/delete")
	public void delete(@RequestParam(value = "id") String id, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		statusService.delete(Integer.parseInt(id));
		result.put("success", true);
		ResponseUtil.write(result, response);
	}
}
